import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyCodeCombination;
import javafx.scene.input.KeyCombination;
import javafx.scene.layout.AnchorPane;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import model.*;
import view.HintTextArea;

public class Main extends Application {

    private IModel model = new ProtocolSZ_2021Model();
    private final Button changeBtn = new Button("转换(Ctrl+Enter)");
    private final HintTextArea inputTv = new HintTextArea();
    private final TextArea outputTv = new TextArea();
    private final Label mVersionLabel = new Label("版本号:2022.07.19 090746");

    private final ChoiceBox<String> mChoiceMode = new ChoiceBox<>(FXCollections.observableArrayList());

    private final Menu mFuncMenu = new Menu("深圳市地方标准-2021");

    @Override
    public void start(Stage stage) throws Exception {
        AnchorPane root = new AnchorPane();
        initMenu(root);
        initTv(root);
        initButton(root);

        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.initStyle(StageStyle.DECORATED);
        stage.setTitle("交通运输行业标准协议解析工具");
        stage.setHeight(600);
        stage.setWidth(800);
        stage.show();

        //监听键盘Ctrl+Enter，执行转换
        KeyCombination kccb = new KeyCodeCombination(KeyCode.ENTER,KeyCombination.CONTROL_DOWN);
        scene.getAccelerators().put(kccb, this::input2output);

        //Ctrl+W可以放大字号
        scene.getAccelerators().put(new KeyCodeCombination(KeyCode.W
                        ,KeyCombination.CONTROL_DOWN)
                , new Runnable() {
            @Override
            public void run() {
                double size = outputTv.getFont().getSize();
                outputTv.setFont(new Font(size+1));
            }
        });

        //Ctrl+S可以缩小字号
        scene.getAccelerators().put(new KeyCodeCombination(KeyCode.S
                ,KeyCombination.CONTROL_DOWN), new Runnable() {
            @Override
            public void run() {
                double size = outputTv.getFont().getSize();
                if (size > 6){
                    outputTv.setFont(new Font(size-1));
                }
            }
        });
    }

    private void initButton(AnchorPane root) {
        AnchorPane.setTopAnchor(changeBtn,240.0);
        AnchorPane.setRightAnchor(changeBtn,50.0);

        AnchorPane.setTopAnchor(mChoiceMode,240.0);
        AnchorPane.setRightAnchor(mChoiceMode,180.0);
        mChoiceMode.getSelectionModel().selectedIndexProperty().addListener((ov,oldv,newv)->{
            model.setType(newv.intValue());
        });

        mChoiceMode.getItems().setAll(model.getTypes());
        mChoiceMode.getSelectionModel().selectFirst();

        changeBtn.setOnMouseClicked(event -> input2output());
        root.getChildren().addAll(changeBtn,mChoiceMode);
    }

    private void input2output() {

        if (inputTv.isEmpty()){
            outputTv.setText("输入内容不能为空");
            return;
        }

        outputTv.setText(model.change(inputTv.getText()));
    }

    private void initTv(AnchorPane root) {
        inputTv.setWrapText(true);
        inputTv.setPrefHeight(200);
        AnchorPane.setTopAnchor(inputTv,30.0);
        AnchorPane.setRightAnchor(inputTv,10.0);
        AnchorPane.setLeftAnchor(inputTv,10.0);

        inputTv.setHint("请输入...");

        outputTv.setWrapText(true);
        outputTv.setPrefHeight(200);
        AnchorPane.setTopAnchor(outputTv,270.0);
        AnchorPane.setRightAnchor(outputTv,10.0);
        AnchorPane.setLeftAnchor(outputTv,10.0);
        AnchorPane.setBottomAnchor(outputTv,20.0);

        mVersionLabel.setWrapText(true);
        mVersionLabel.setPrefHeight(10);
        AnchorPane.setRightAnchor(mVersionLabel,10.0);
        AnchorPane.setBottomAnchor(mVersionLabel,2.0);

        Label tip = new Label("Tip:Ctrl+W可放大字号,Ctrl+S可缩小字号");
        tip.setWrapText(true);
        tip.setPrefHeight(10);
        AnchorPane.setLeftAnchor(tip,10.0);
        AnchorPane.setBottomAnchor(tip,2.0);

        root.getChildren().addAll(inputTv,outputTv, mVersionLabel,tip);
    }

    private void initMenu(AnchorPane root) {
        MenuBar menuBar = new MenuBar();
        menuBar.setStyle("-fx-background-radius:5");

        mFuncMenu.setStyle("-fx-background-radius: 20");

        MenuItem js2017 = new MenuItem("江苏外设通讯协议-2017");
        MenuItem sz2021 = new MenuItem("深圳市地方标准-2021");
        MenuItem jt905_2014 = new MenuItem("JT/905-2014");
        MenuItem jt1078_2016 = new MenuItem("JT/1078-2016");
        MenuItem jt808_2019 = new MenuItem("JT/808-2019");
        MenuItem jt808_2011 = new MenuItem("JT/808-2011");

        mFuncMenu.getItems().addAll(
                sz2021
                ,jt905_2014
                ,jt1078_2016
                ,js2017
                ,jt808_2019
                ,jt808_2011
        );
        menuBar.getMenus().addAll(mFuncMenu);

        root.getChildren().add(menuBar);
        AnchorPane.setTopAnchor(menuBar,3.0);
        AnchorPane.setLeftAnchor(menuBar,10.0);

        js2017.setOnAction(event -> {
            clickMenu(js2017,new ProtocolJS_2017Model());
        });

        sz2021.setOnAction(event -> {
            clickMenu(sz2021,new ProtocolSZ_2021Model());
        });

        jt905_2014.setOnAction(event -> {
            clickMenu(jt905_2014,new Protocol905_2014Model());
        });

        jt1078_2016.setOnAction(event -> {
            clickMenu(jt1078_2016,new Protocol1078_2016Model());
        });

        jt808_2019.setOnAction(event -> {
            clickMenu(jt808_2019,new Protocol808_2019Model());
        });

        jt808_2011.setOnAction(event -> {
            clickMenu(jt808_2011,new Protocol808_2011Model());
        });
    }

    private void clickMenu(MenuItem v,IModel m){
        mFuncMenu.setText(v.getText());
        model = m;
        mChoiceMode.getItems().setAll(model.getTypes());
        mChoiceMode.getSelectionModel().selectFirst();
    }

    public static void main(String[] args) {
        launch();
    }
}
